package br.com.millenium.domain.manager;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.persistence.Query;

import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.portal.manager.ManagerFactory;
import lumis.util.ITransaction;

public class FerramentaManager implements IFerramentaManager 
{
	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFuncionarioManager#isDeleteableCargo(java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDeleteableTipoFerramenta( String tipoFerramentaId, ITransaction transaction ) throws PortalException 
	{
		boolean returnValue = false;
		
		try 
		{
			String sql = "SELECT COUNT(id) AS total FROM MM_Ferramenta WHERE tipoFerramentaId = ?";
			Query query = ManagerFactory.getEntityManager().createNativeQuery(sql);
			query.setParameter(1, tipoFerramentaId);
			
			BigInteger result  = (BigInteger) query.getSingleResult();
			returnValue = (result.intValue() == 0);
		}
		catch (Exception e) 
		{
			throw new UnexpectedException(e);
		}
		
		return returnValue;
	}
	

	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFerramentaManager#isDuplicateTipoFerramenta(java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDuplicateTipoFerramenta(String tipoFerramentaId, String nome, ITransaction transaction) throws PortalException 
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc)transaction;
			Connection       connection         = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement( "SELECT id FROM MM_TipoFerramenta WHERE nome = ? and id <> ?" );
			try
			{
				statement.setString( 1, nome             );
				statement.setString( 2, tipoFerramentaId );
				
				ResultSet rs = statement.executeQuery();
				if( rs.next() )
					return true;
				else
					return false;
			}
			finally
			{
				statement.close();
			}
		}
		catch (Exception e)
		{
			throw new PortalException(e.getMessage());
		}
	}


	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFerramentaManager#isDuplicateFerramenta(java.lang.String, java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDuplicateFerramenta(String numRegistro, String ferramentaId, ITransaction transaction)	throws PortalException 
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc)transaction;
			Connection       connection         = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement( "SELECT id FROM MM_Ferramenta WHERE numRegistro = ? and id <> ?" );
			try
			{
				statement.setString( 1, numRegistro  );
				statement.setString( 2, ferramentaId );
				
				ResultSet rs = statement.executeQuery();
				if( rs.next() )
					return true;
				else
					return false;
			}
			finally
			{
				statement.close();
			}
		}
		catch (Exception e)
		{
			throw new PortalException(e.getMessage());
		}
	}

	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFerramentaManager#isDeleteableFerramenta(java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDeleteableFerramenta(String ferramentaId, ITransaction transaction) throws PortalException 
	{
		boolean returnValue = false;
		
		try 
		{
			String sql = "SELECT COUNT(id) AS total FROM MM_AlocacaoFerramenta WHERE ferramentaId = ?";
			Query query = ManagerFactory.getEntityManager().createNativeQuery(sql);
			query.setParameter(1, ferramentaId);
			
			BigInteger result  = (BigInteger) query.getSingleResult();
			returnValue = (result.intValue() == 0);
		}
		catch (Exception e) 
		{
			throw new UnexpectedException(e);
		}
		
		return returnValue;
	}


	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFerramentaManager#isDuplicateAlocacaoFerramenta(java.lang.String, java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDuplicateAlocacaoFerramenta(String numRegistro, String alocacaoFerramentaId, ITransaction transaction) throws PortalException 
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc)transaction;
			Connection       connection         = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement( "SELECT id FROM MM_AlocacaoFerramenta WHERE ferramentaId = ? and dataDevolucao is NULL " + (alocacaoFerramentaId == null ? "" : "and id = ?"));
			try
			{
				statement.setString( 1, numRegistro );
				
				if( alocacaoFerramentaId != null )
					statement.setString( 2, alocacaoFerramentaId );
				
				ResultSet rs = statement.executeQuery();
				if( rs.next() )
					return true;
				else
					return false;
			}
			finally
			{
				statement.close();
			}
		}
		catch (Exception e)
		{
			throw new PortalException(e.getMessage());
		}
	}
}
